package com.kd.juhaowan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.hjq.toast.ToastUtils;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.GameDjqApi;
import com.kd.juhaowan.http.api.ReciveDjqApi;
import com.kd.juhaowan.http.model.GameDjqModel;
import com.kd.juhaowan.http.model.GameModel;
import com.kd.juhaowan.http.model.GiftModel;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.http.model.HttpListData;
import com.kd.juhaowan.ui.adapter.BaseRecyclerViewAdapter;
import com.kd.juhaowan.ui.adapter.BaseViewHolder;
import com.kd.juhaowan.ui.holder.GameDjqViewHolder;
import com.kd.juhaowan.ui.listener.OnItemDianzanListener;
import com.kd.juhaowan.utils.DeviceUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;
import butterknife.OnClick;

public class GameDjqActivity extends BaseRefreshActivity implements OnItemDianzanListener {

    @BindView(R.id.view_refreshlist_refresh)
    SmartRefreshLayout refreshLayout;





    @BindView(R.id.gift_listview)
    RecyclerView recyclerView;

    private BaseRecyclerViewAdapter<GameDjqViewHolder, GameDjqModel> giftAdapter;
    @BindView(R.id.activity_title)
    TextView titleView;
    @BindView(R.id.activity_more)
    TextView moreView;
    @BindView(R.id.gamedjq_tips)
    TextView tipView;


    @Override
    protected SmartRefreshLayout getSmartRefreshLayout() {
        return refreshLayout;
    }

    @Override
    protected String getSmartRefreshTaskTag() {
        return null;
    }

    @Override
    protected void onRequestNextPageData(int page) {
        EasyHttp.post(this).api(new GameDjqApi(gameModel.getId(),page)).request(new HttpCallbackProxy<HttpListData<GameDjqModel>>(this){
            @Override
            public void onHttpSuccess(HttpListData<GameDjqModel> result, boolean cache) {
                super.onHttpSuccess(result, cache);
                if (page==1){
                    tipView.setText(result.getData().getClaim_reminder());
                    giftAdapter.resetAdapterData(result.getData().getList());
                }else {
                    giftAdapter.appendAdapterData(result.getData().getList());
                }

            }
        });
    }


    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {

    }

    @Override
    public void onItemDianzanClick(BaseViewHolder viewHolder, int position) {
        GameDjqModel model = giftAdapter.getItem(position);
        EasyHttp.post(this).api(new ReciveDjqApi(model.getId(), DeviceUtil.isInstallApp(this,gameModel.getGame_package_name())?1:2)).request(new HttpCallbackProxy<HttpData<GiftModel>>(this){
            @Override
            public void onHttpSuccess(HttpData<GiftModel> result, boolean cache) {
                super.onHttpSuccess(result, cache);
                model.setIs_receive(1);
//                    model.setCode(result.getMessage());
                giftAdapter.notifyItemChanged(position,model);
                ToastUtils.show("领取成功");
            }
        });
    }

    @Override
    protected boolean isNullData(HttpData response) {
        return false;
    }
    private GameModel gameModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gameModel = (GameModel)getIntent().getSerializableExtra("game");
//        gameid = getIntent().getStringExtra("game_id");
//        gameName = getIntent().getStringExtra("game_name");
        titleView.setText(gameModel.getName()+"-代金券");
        moreView.setText("我的代金券");
        initAdapter();
        onRefresh(getSmartRefreshLayout());
//        setContentView(R.layout.activity_account_login);

    }

    @Override
    protected void reInitDate() {
        onRefresh(getSmartRefreshLayout());
    }

    @OnClick(R.id.actvity_back)
    public void onBackClick(){
        finish();
    }

    @OnClick(R.id.activity_more)
    public void onMoreClick(){
        startActivity(new Intent(GameDjqActivity.this,MineDjqActivity.class));
    }
    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.activity_game_djq;
    }

    private void initAdapter(){
        giftAdapter = new BaseRecyclerViewAdapter<GameDjqViewHolder, GameDjqModel>() {
            @NonNull
            @Override
            public GameDjqViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                View view = inflater.inflate(R.layout.item_game_djq, null, false);
                GameDjqViewHolder holder=new GameDjqViewHolder(view);
                holder.setOnItemClickListener(getOnItemClickListener());
                return  holder;

            }

            @Override
            public void onBindViewHolder(@NonNull GameDjqViewHolder holder, int position) {
                holder.setDate(getItem(position),position == getItemCount()-1);

            }
        };
        giftAdapter.setOnItemClickListener(this);
        giftAdapter.resetAdapterData(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(giftAdapter);



    }


}
